package com.example.myapplication




import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.shipinViewholder

class shipinAdapter : RecyclerView.Adapter<shipinViewholder> {

    /** 保存数据的列表 */
    private val demoList: List<shipinitem>

    constructor(demoList: List<shipinitem>) {
        this.demoList = demoList
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): shipinViewholder {
        val view: View =
            LayoutInflater.from(parent.context).inflate(R.layout.shipin_item, parent, false)
        val viewHolder:shipinViewholder=shipinViewholder(view)

        // 添加点击事件处理
        viewHolder.ivCover.setOnClickListener {
            val position = viewHolder.adapterPosition
            val context = viewHolder.itemView.context

            // 获取对应视频的URL
            val videoUrl = demoList[position].videoUrl

            // 跳转到视频播放Activity，并传递视频URL
            val intent = Intent(context, VideoPlayerActivity::class.java)
            intent.putExtra("video_url", videoUrl)
            context.startActivity(intent)
        }



        return viewHolder
    }




    override fun onBindViewHolder(holder: shipinViewholder, position: Int) {
        val itemBean = demoList[position]

        itemBean.coverUrl?.let {
            holder.ivCover?.setImageResource(it)
        }

        itemBean.title?.let {
            holder.tvTitle.text = it
        }

        itemBean.content?.let {
            holder.tvContent.text = it
        }
        itemBean.coverUrl2?.let {
            holder.ivCover2?.setImageResource(it)
        }
        itemBean.title2?.let {
            holder.tvTitle2.text = it
        }
    }


    override fun getItemCount(): Int {
        return demoList.size
    }
}
